#include "comm.hpp"
#include <unistd.h>
#include <cstring>

int main()
{
    // cout << "server: " << getkey() << endl;
    // cout << "server: " << createShm(getkey(), SIZE) << endl;

    // int shmid = createShm(getkey(), SIZE);

    // char* start = attachShm(shmid);

    // while(1)
    // {
    //     cout << "client :" << start << endl;
    //     if(start[strlen(start) - 1] == 'Z')
    //         break;

    //     sleep(1);
    // }

    // detaShm(start);

    // delShm(shmid);

    Init ser(SERVER);
    char* start = ser.GetStart();

    int* fd = ser.PipeOpen();
    cout << ready << endl;
    char buff[1024];
    while(1)
    {
        ssize_t n = read(fd[0], buff, sizeof(buff));

        if(n == 0) break; // 写端关闭，自动退出循环
        // 通过共享内存通信
        cout << "client: " << start << endl;

        write(fd[1], ready, strlen(ready));
    }

    return 0;
}